package com.dengke.springsecurity.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("com.dengke.springsecurity.*")
@EnableTransactionManagement
@PropertySource("classpath:datasource-cfg.properties")
public class ApplicationContextConfig {
	@Autowired
	private Environment environment;

	@Bean
	public ResourceBundleMessageSource messageSource() {
		ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
		// load property in message/validator.properties
		rb.setBasenames(new String[] { "message/validator" });
		return rb;
	}

	@Bean
	public InternalResourceViewResolver getViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setPrefix("/WEB-INF/pages/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

	@Bean(name = "dataSource")
	public DataSource getDataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();

		// See: datasouce-cfg.properties
		dataSource.setDriverClassName(environment.getProperty("ds.database-driver"));
		dataSource.setUrl(environment.getProperty("ds.url"));
		dataSource.setUsername(environment.getProperty("ds.username"));
		dataSource.setPassword(environment.getProperty("ds.password"));

		System.out.println("## getDataSource: " + dataSource);

		return dataSource;
	}

	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver viewResolver = new CommonsMultipartResolver();
		viewResolver.setMaxUploadSize(1 * 1024 * 1024);
		return viewResolver;
	}

	@Autowired
	@Bean(name = "sessionFactroy")
	public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
		System.out.println("### getSessionFactory......");
		try {
			Properties properties = new Properties();
			// See: ds-hibernate-cfg.properties
			properties.put("hibernate.dialect", environment.getProperty("hibernate.dialect"));
			properties.put("hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
			properties.put("current_session_context_class", environment.getProperty("current_session_context_class"));

			LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

			// Package contain entity classes
			factoryBean.setPackagesToScan(new String[] { "com.dengke.springsecurity.entity" });
			factoryBean.setDataSource(dataSource);
			factoryBean.setHibernateProperties(properties);
			factoryBean.afterPropertiesSet();
			//
			SessionFactory sf = factoryBean.getObject();
			System.out.println("## getSessionFactory: " + sf);
			return sf;
		} catch (Exception e) {
			System.out.println("Error getSessionFactory: " + e);
			e.printStackTrace();
			throw e;
		}
	}

	// // Transaction Manager
	// @Autowired
	// @Bean(name = "transactionManager")
	// public DataSourceTransactionManager getTransactionManager(DataSource
	// dataSource) {
	// DataSourceTransactionManager transactionManager = new
	// DataSourceTransactionManager(dataSource);
	//
	// return transactionManager;
	// }

	// Hibernate Transaction Manager

	// 此处注意使用的包（4 not 3）
	@Autowired
	@Bean(name = "transactionManager")
	public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
		HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

		return transactionManager;
	}
}
